package com.bosssoft.cart.service.impl;

import com.bosssoft.cart.mapper.BookMapper;
import com.bosssoft.cart.mapper.ShopCartMapper;
import com.bosssoft.cart.pojo.Books;
import com.bosssoft.cart.pojo.ShopCart;
import com.bosssoft.cart.service.ShopCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ShopCartServiceImpl implements ShopCartService {

    @Autowired
    private ShopCartMapper shopCartMapper;

    @Autowired
    private BookMapper bookMapper;

    /**
     * 获取购物车列表
     * @return 购物车
     */
     @Override
    public List<ShopCart> queryCartList() {
        return shopCartMapper.queryCartList();
    }

    /**
     * 根据id从购物车移除商品
     * @param bookID 商品id
     */
    @Override
    public void removeFromCart(int bookID,HttpSession session) {

        ShopCart shopCart = shopCartMapper.queryShopByShopID(bookID);

        Map map = (Map)session.getAttribute("carts");

        map.remove(shopCart);

    }

    /**
     * 根据id将商品添加进购物车
     * @param bookID 商品id
     */
    @Override
    public void addIntoCart(int bookID, HttpSession session) {

        Books books = bookMapper.queryBookById(bookID);

        ShopCart shopCart = new ShopCart();

        shopCart.setShopID(books.getBookID());

        shopCart.setShopName(books.getBookName());

        HashMap<String, ShopCart> map = new HashMap<>();

        map.put("carts",shopCart);

        session.setAttribute("carts",map);

    }

    /**
     * 根据id将商品添加进购物车
     * @param id 商品id
     */
    @Override
    public ShopCart queryShopByShopID(int id) {
        return shopCartMapper.queryShopByShopID(id);
    }
}
